<?php

namespace App\Logic;

use App\Models\User;
use Illuminate\Support\Facades\Hash;

class UserLogic
{

    /**
     * 登录验证
     * @param $email
     * @param $password
     * @return array
     */
    public function checkPassword($email, $password)
    {

        $userMdl = new User;
        $userinfo = $userMdl->where(['email' => $email])->select(['id', 'name', 'head_img', 'email', 'password', 'api_token'])->first();
        if (empty($userinfo)) {
            return ['code' => 0, 'msg' => '邮箱不存在'];
        }
        if (!Hash::check($password, $userinfo['password'])) {
            return ['code' => 0, 'msg' => '密码错误'];
        }
        unset($userinfo['password']);
        $userinfo['api_token'] = md5($userinfo['email'] . time() . mt_rand(10, 99));
        $userMdl->where(['email' => $email])->update(['api_token' => $userinfo['api_token']]);

        $userMdl->delUser($userinfo['id']); // 清除用户缓存数据

        return ['code' => 1, 'data' => $userinfo];
    }

}
